﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NSuggest.Jsonp.Core;

namespace NSuggest.Example.Cats
{
	/// <summary>
	/// An example of how to use JsonpEngine inside a IHttpHandler.
	/// </summary>
	public class CatsJsonpService : JsonpHandler
	{
		private readonly Cat[] _cats = new[]
										   {
											   new Cat("Tiger", "Panthera Tigris"),
											   new Cat("Lion", "Panthera Leo"),
											   new Cat("Jaguar", "Panthera Onca"),
											   new Cat("Leopard", "Panthera Pardus"),
											   new Cat("Snow Leopard", "Panthera Uncia"),
											   new Cat("Mountain Lion", "Puma Concolor"),
											   new Cat("Cheetah", "Acinonyx Jubatus"),
											   new Cat("Lynx", "Lynx Lynx"),
										   };

		protected override bool IsAuthorized(HttpContext context)
		{
			return true;
		}

		protected override object GetObject(HttpContext context)
		{
			return SearchForCats(context);
		}

		public IEnumerable<Cat> SearchForCats(HttpContext context)
		{
			var query = context.Request.QueryString["q"].ToLower();

			var tokens = query.Split(' ').Where(x => !string.IsNullOrEmpty(x));
			var and = context.Request.QueryString["and"];
			IEnumerable<Cat> cats;
			if (and != null && and == "1")
			{
				cats = _cats.Where(cat => MatchesAllTokens(cat, tokens));
			}
			else
			{
				cats = _cats
					.Select(cat => new { Cat = cat, Rank = Rank(cat, tokens) })
					.Where(x => x.Rank > 0)
					.OrderByDescending(x => x.Rank)
					.Select(x => x.Cat);

			}
			return cats.ToList();
		}

		private static bool MatchesAllTokens(Cat cat, IEnumerable<string> tokens)
		{
			return !tokens.Any(token => !cat.EnglishName.ToLower().Contains(token) && !cat.LatinName.ToLower().Contains(token));

		}

		private static int Rank(Cat cat, IEnumerable<string> tokens)
		{
			return tokens.Count(
				token => cat.EnglishName.ToLower().Contains(token) || cat.LatinName.ToLower().Contains(token));
		}
	}
}